package com.bvgol.examples.springbootmybatisxmlannotion.dao;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.bvgol.examples.springbootmybatisxmlannotion.common.MyPage;
import com.bvgol.examples.springbootmybatisxmlannotion.entity.SysRoleDept;
import com.bvgol.examples.springbootmybatisxmlannotion.service.SysRoleDeptService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname SysRoleDeptDaoTest
 * @Description 测试分页
 * @Date 2020/5/6 16:37
 * @Created by guochen
 */
@Slf4j
@SpringBootTest
public class SysRoleDeptDaoTestPage {
    @Resource
    private SysRoleDeptDao sysRoleDeptDao;

    @Resource
    private SysRoleDeptService sysRoleDeptService;

    /*
    mybatis 分页都有几种实现
    1.拦截器分页
        4.Mybatis分页插件PageHelper
        5.通用mapper?
    2.内存分页
    3.sql 分页

     */

    @Test
    public void queryAll() {
        List<SysRoleDept> sysRoleDepts = sysRoleDeptDao.queryAll(null);
        Assert.assertEquals(5, sysRoleDepts.size());
        sysRoleDepts.forEach(System.out::println);
    }

    @Test
    public void testInit() {
        for (int i = 1; i < 112; i++) {
            SysRoleDept sysRoleDept = new SysRoleDept();
            sysRoleDept.setId(IdUtil.getSnowflake(1L, 1L).nextId());
            sysRoleDept.setRoleId(Long.valueOf(i));
            sysRoleDept.setDeptId(Long.valueOf(i));

            int insert = sysRoleDeptDao.insert(sysRoleDept);
        }
        System.out.println("----------------------------------");
    }

    @Test
    public void testPage() {
        MyPage myPage = MyPage.builder().pageSize(10).current(1).build();
        SysRoleDept sysRoleDept = new SysRoleDept();
        int totalCount = sysRoleDeptDao.myPageCount(sysRoleDept);
        List<SysRoleDept> sysRoleDepts = sysRoleDeptDao.myPage(sysRoleDept,myPage);

        myPage.setData(sysRoleDepts);
        myPage.setTotalCount(totalCount);
        System.out.println(JSON.toJSONString(myPage));
    }

    @Test
    public void test_pagehelper() {
//        PageHelper.startPage(1, 20);
//        SysRoleDept sysRoleDept = new SysRoleDept();
//        List<SysRoleDept> sysRoleDepts = sysRoleDeptDao.queryAll(sysRoleDept);
//        System.out.println("Total: " + ((Page) sysRoleDepts).getTotal());
//        for (SysRoleDept sysRoleDept2 : sysRoleDepts) {
//            System.out.println("Name: " + sysRoleDept2.getDeptId());
//        }
    }
}


